package com.medicine.controller;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

import com.medicine.model.IllnessCategory;
import com.medicine.model.SubCategory;

@ViewScoped
@Named
public class SubCategoryController implements Serializable{
	
	private static final long serialVersionUID = -4217378360090646369L;
	
	@Inject SubCategory subCategory;
	@PersistenceContext EntityManager em;
	@Inject UserTransaction ut;
	@Inject SubCategory selectedBean;
	
	private List<IllnessCategory> illNessCategoryList;
	private List<SubCategory> subCategoryList;
	private int selectedIcType;
	
	@SuppressWarnings("unchecked")
	@PostConstruct
	public void postConstruct(){
		illNessCategoryList = em.createQuery("select cc from IllnessCategory cc").getResultList();
		queryInstance();
	}
	
	public void createInstance() throws Exception{
		ut.begin();
		em.joinTransaction();
		SubCategory sc = new SubCategory();
		for (IllnessCategory ic: illNessCategoryList){
			if (ic.getId() == selectedIcType){
				sc.setIllnessCategory(ic);
			}
		}
		sc.setMainSolution(subCategory.getMainSolution());
		sc.setSubCateName(subCategory.getSubCateName());
		em.persist(sc);
		ut.commit();
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("操作已完成"));
		queryInstance();
	}
	
	@SuppressWarnings("unchecked")
	public void queryInstance(){
		subCategoryList = em.createQuery("select cc from SubCategory cc order by cc.illnessCategory.id").getResultList();
	}
	
	public void updateInstance() throws Exception{
		ut.begin();
		em.joinTransaction();
		SubCategory sc = new SubCategory();
		sc.setId(selectedBean.getId());
		sc.setIllnessCategory(selectedBean.getIllnessCategory());
		sc.setMainSolution(selectedBean.getMainSolution());
		sc.setSubCateName(selectedBean.getSubCateName());
		em.persist(em.merge(sc));
		ut.commit();
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("操作已完成"));
		queryInstance();
	}
	
	public void loadSelectedBean(int id){
		for (SubCategory bean: subCategoryList){
			if (bean.getId() == id){
				selectedBean = bean;
				break;
			}
		}
	}

	public SubCategory getSubCategory() {
		return subCategory;
	}

	public void setSubCategory(SubCategory subCategory) {
		this.subCategory = subCategory;
	}

	public SubCategory getSelectedBean() {
		return selectedBean;
	}

	public void setSelectedBean(SubCategory selectedBean) {
		this.selectedBean = selectedBean;
	}

	public List<SubCategory> getSubCategoryList() {
		return subCategoryList;
	}

	public void setSubCategoryList(List<SubCategory> subCategoryList) {
		this.subCategoryList = subCategoryList;
	}

	public List<IllnessCategory> getIllNessCategoryList() {
		return illNessCategoryList;
	}

	public void setIllNessCategoryList(List<IllnessCategory> illNessCategoryList) {
		this.illNessCategoryList = illNessCategoryList;
	}

	public int getSelectedIcType() {
		return selectedIcType;
	}

	public void setSelectedIcType(int selectedIcType) {
		this.selectedIcType = selectedIcType;
	}

}
